$(document).ready(function() {
    // fuelux datagrid
    var DataGridDataSource = function (options) {
        this._formatter = options.formatter;
        this._columns = options.columns;
        this._delay = options.delay;
    };

    DataGridDataSource.prototype = {

        columns: function () {
            return this._columns;
        },

        data: function (options, callback) {
            var url = 'http://120.24.42.44:8888/boss/admin/developer/showadpos';
            var self = this;
            var adminAccount = parseInt(localStorage.id);
            setTimeout(function () {

                var data = $.extend(true, [], self._data);

                $.ajax(url, {
                    data:{
                       aid :adminAccount
                    },
                    dataType: 'json',
                    async: false,
                    type: 'GET'
                }).done(function (response) {

                    data = response;

                    // SEARCHING 查找
                    if (options.search) {
                        data = _.filter(data, function (item) {
                            var match = false;

                            _.each(item, function (prop) {
                                if (_.isString(prop) || _.isFinite(prop)) {
                                    if (prop.toString().toLowerCase().indexOf(options.search.toLowerCase()) !== -1) match = true;
                                }
                            });

                            return match;
                        });
                    }

                    // FILTERING 过滤
                    if (options.filter) {
                        data = _.filter(data, function (item) {
                            switch(options.filter.value) {
                                case 'lt5m':
                                    if(item.population < 5000000) return true;
                                    break;
                                case 'gte5m':
                                    if(item.population >= 5000000) return true;
                                    break;
                                default:
                                    return true;
                                    break;
                            }
                        });
                    }

                    var count = data.length;

                    // SORTING 排序
                    if (options.sortProperty) {
                        data = _.sortBy(data, options.sortProperty);
                        if (options.sortDirection === 'desc') data.reverse();
                    }

                    // PAGING
                    var startIndex = options.pageIndex * options.pageSize;
                    var endIndex = startIndex + options.pageSize;
                    var end = (endIndex > count) ? count : endIndex;
                    var pages = Math.ceil(count / options.pageSize);
                    var page = options.pageIndex + 1;
                    var start = startIndex + 1;

                    data = data.slice(startIndex, endIndex);

                    if (self._formatter) self._formatter(data);

                    callback({ data: data, start: start, end: end, count: count, pages: pages, page: page });
                }).fail(function(e){

                });
            }, self._delay);
        }
    };

    $('#MyStretchGrid').each(function() {
        $(this).datagrid({
            dataSource: new DataGridDataSource({
                // Column definitions for Datagrid
                columns: [
                    {
                        property:'did',
                        label: '开发者账号',
                        sorttable: true
                    },
                    {
                        property: 'adpositionid',
                        label: '广告位ID',
                        sortable: true
                    },
                    {
                        property: 'pwebsiteurl',
                        label: '网络地址',
                        sortable: true
                    },
                    {
                        property: 'pwebsitenum',
                        label: '网站备案号',
                        sortable: true
                    },
                    {
                        property: 'pvalidateStatus',
                        label: '状态',
                        sortable: true
                    },
                    {
                        property: 'action',
                        label: '操作',
                        sortable: true
                    }
                ],

                // Create IMG tag for each returned image
                formatter: function (items) {

                    $.each(items, function (index, item) {

                        if(item.pvalidateStatus === 2){
                            item.action = '<button class="btn btn-info btn-small"   onclick="pass(this)">审核通过<!--<i class="icon-pencil"></i>--></button>';
                            item.pvalidateStatus = "审核不通过";
                        }
                        if(item.pvalidateStatus === 1){
                            //item.action = '<button class="btn btn-primary btn-small" style = "background: red"  onclick="thaw(this)"><!--<i class="icon-pencil"></i>--></button>';
                            item.action = '<label>   </label>';
                            item.pvalidateStatus = "审核通过";

                        }
                        if(item.pvalidateStatus === 0){
                            item.action = '<button class="btn btn-info btn-small" onclick="pass(this)">审核通过<!--<i class="icon-pencil"></i>--></button>';
                            item.action += '<button class="btn btn-danger btn-small"  onclick="noPass(this)">审核不通过<!--<i class="icon-pencil"></i>--></button>';
                            item.pvalidateStatus = "待审核";
                        }
                        //item.geonameId = '<button href="#edit?geonameid='+item.geonameId+'"><i class="icon-pencil"></i></button>';
                        //item.geonameId = '<button class="btn btn-primary">修改<i class="icon-pencil"></i></button>';
                        //item.geonameId = '<button class="btn btn-primary">修改<i class="icon-pencil"></i></button>';
                        //item.geonameId+='<button class="btn btn-danger" onclick="remove(this)">删除<i class="icon-remove-sign"></i></button>';

                    });
                }
            })
        });
    });

});


function freeze(e){
    //找到父节点
    var parent = $(e).parent();
    //和父节点同级的上一层节点状态改为解冻
    parent.prev().text("使用中");
    //button的text改为冻结，同时更改绑定函数
    $(e).text("冻结");
    $(e).attr('onclick', '').bind('click', function(){ thaw(e); });

/*    var developerAccount = parseInt(parent.prev().prev().html());
    var adminAccount = parseInt(localStorage.id);
    var order = parseInt(parent.prev().prev().prev().prev().prev().html());
    url = localStorage.url + '/boss/admin/developer/withdrawconfirm';*/
    //wid是在数据库中申请单的序列号，如何映射到html中
    var jqxhr = $.get(url,{
        did: developerAccount,
        aid: adminAccount,
        wid: order,
        status: 1
    } ).success(function(){
        alert('解冻成功');
    })


}

function thaw(e){
    //找到父节点
    var parent = $(e).parent();
    //和父节点同级的上一层节点状态改为冻结
    parent.prev().text("冻结");
    //button的text改为解冻，同时更改绑定函数
    $(e).text("解冻");
    $(e).attr('onclick', '').bind('click', function(){ freeze(e); });

/*    var developerAccount = parseInt(parent.prev().prev().html());
    var adminAccount = parseInt(localStorage.id);
    var order = parseInt(parent.prev().prev().prev().prev().prev().html());
    url = localStorage.url + '/boss/admin/developer/withdrawconfirm';*/
    //wid是在数据库中申请单的序列号，如何映射到html中
    var jqxhr = $.get(url,{
        did: developerAccount,
        aid: adminAccount,
        wid: order,
        status: 1
    } ).success(function(){

    })


}

function pass(e){
    var parent = $(e).parent();
    //和父节点同级的上一层节点状态改为冻结
    parent.prev().text("审核通过");
    //button的text改为解冻，同时更改绑定函数
    //$(e).text("冻结");
    //$(e).css("backgroundColor", "red");
    //$(e).attr('onclick', '').bind('click', function(){ thaw(e); });
    //同时删除下一个按钮
    $(e).fadeOut('slow');
    $(e).next().fadeOut('slow');

    var adminAccount = parseInt(localStorage.id);
    var adid = parseInt(parent.prev().prev().prev().prev().html());
    var developerAccount=parseInt(parent.prev().prev().prev().prev().prev().html());
/*    var developerAccount = parseInt(parent.prev().prev().html());
    var adminAccount = parseInt(localStorage.id);
    var order = parseInt(parent.prev().prev().prev().prev().prev().html());
    url = localStorage.url + '/boss/admin/developer/withdrawconfirm';*/
    //wid是在数据库中申请单的序列号，如何映射到html中
    var url = 'http://120.24.42.44:8888/boss/admin/developer/confirmadpos';
    var jqxhr = $.get(url,{
        did: developerAccount,
        aid: adminAccount,
        adpositionid: adid,
        type: 1
    } ).success(function(){
        //alert('success')
    })


}

function noPass(e){
    var parent = $(e).parent();
    //和父节点同级的上一层节点状态改为冻结
    parent.prev().text("审核不通过");
    $(e).fadeOut('slow');
    $(e).prev().fadeOut('slow');
    //button的text改为解冻，同时更改绑定函数
    /*$(e).text("解冻");
    $(e).css("backgroundColor", "#13c4a5");
    $(e).attr('onclick', '').bind('click', function(){ freeze(e); });*/

    var adminAccount = parseInt(localStorage.id);
    var adid = parseInt(parent.prev().prev().prev().prev().html());
    var developerAccount=parseInt(parent.prev().prev().prev().prev().prev().html());
    /*    var developerAccount = parseInt(parent.prev().prev().html());
        var adminAccount = parseInt(localStorage.id);
        var order = parseInt(parent.prev().prev().prev().prev().prev().html());
        url = localStorage.url + '/boss/admin/developer/withdrawconfirm';*/
    //wid是在数据库中申请单的序列号，如何映射到html中
    var url = 'http://120.24.42.44:8888/boss/admin/developer/confirmadpos';
    var jqxhr = $.get(url,{
        did: developerAccount,
        aid: adminAccount,
        adpositionid: adid,
        type: 2
    } ).success(function(){

    })
}